package com.zx.ishopping.Task;

import com.zx.ishopping.entity.Order;
import com.zx.ishopping.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单定时任务
 */
//配置类
@Component
@Slf4j
public class OrderTask {
    
    @Autowired
    private OrderMapper orderMapper;
    
    //每分钟检查一次订单是否过期
    @Scheduled(cron = "0 * * * * ?")
    public void expiresOrderTask(){
        log.info("开始检查是否有超时未支付订单:{}", LocalDateTime.now());
        //若订单状态处于未支付，十五分钟后未支付自动取消。
        //设置超时时间
        LocalDateTime expireTime = LocalDateTime.now().plusMinutes(-15);
        //查询状态为未支付的超时订单
        List<Order> orderList = orderMapper.queryExpireOrderList(expireTime,0);
        //此时说明查询到了超时订单
        if(orderList != null && orderList.size() > 0){
            //将超时订单更新为已取消
            for (Order order : orderList){
                order.setStatus(4);
                orderMapper.updateOrder(order);
                log.info("超时订单处理完成");
            }
        }else {
            log.info("无超时订单");
        }
        
    }
    
    
    
}
